package com.rosense.srpingboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rosense.srpingboot.aspect.NotAuthorize;
import com.rosense.srpingboot.cache.WebContextUtil;
import com.rosense.srpingboot.entity.Result;
import com.rosense.srpingboot.entity.UserEntity;
import com.rosense.srpingboot.service.IUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author 李岩飞
 * @email eliyanfei@126.com
 * @date 2018年3月10日 上午11:18:56
 */
@Api(tags = "登录管理")
@Controller
@RequestMapping("login")
public class LoginController extends BaseController {
	@Autowired
	private IUserService userService;

	@NotAuthorize
	@ApiOperation(value = "登录")
	@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public Result login(String account, String password) {
		UserEntity user = userService.get(account, password);
		if (user == null) {
			return Result.error("用户名错误");
		}
		WebContextUtil.login(user);
		addLog("login");
		return Result.ok();
	}

	@ApiOperation(value = "注销")
	@RequestMapping(value = "logout", method = RequestMethod.POST)
	@ResponseBody
	public Result logout() {
		addLog("logout");
		WebContextUtil.logout();
		return Result.ok();
	}
}
